<template>
  <div>
    <el-table
      :data="addressList"
      border
      v-loading="addressListLoading"
      style="width: 100%;"
    >
      <el-table-column prop="id" v-if="false"> </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="收件人"
      >
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        label="手机号码"
      >
      </el-table-column>
      <el-table-column
        prop="province,city,district"
        header-align="center"
        align="center"
        label="所在地区"
        width="200px"
      >
        <template slot-scope="scope">
          <span
            >{{ scope.row.province }}&nbsp;{{ scope.row.city }}&nbsp;{{
              scope.row.district
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="addr"
        header-align="center"
        align="center"
        label="详细地址"
      >
      </el-table-column>
      <el-table-column
        prop="is_default"
        label="设为默认收货地址"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.is_default"
            >{{ scope.row.isDefault }}
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="80"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row.id)"
          ></el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 修改个人信息 -->
    <address-update
      v-if="editVisible"
      ref="addressUpdate"
      @refreshAddresInfo="getAddressInfo"
    ></address-update>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5, 10]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      user_id: '',
      addressList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addressListLoading: false
    }
  },
  activated() {
    // 从vuex中获取个人信息!!误删
    this.getMallUserInfo()
    this.getAddressList()
  },
  methods: {
    // 从vuex中获取个人信息!!误删
    getMallUserInfo() {
      this.user_id = this.$store.getters.getUser.id
    },
    // 获取地址列表
    getAddressList() {
      this.addressListLoading = true
      this.$http({
        url: this.$http.adornUrl('/member/address/list'),
        method: 'get',
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          user_id: this.user_id
        })
      }).then(({ data }) => {
        console.log(data)
        if (data && data.code === 0) {
          console.log(data)
          this.addressList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.addressList = []
          this.totalPage = 0
        }
        this.addressListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getAddressList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getAddressList()
    }
  }
}
</script>
<style scoped></style>
